//
// Created by gushen610140 on 2024/5/6.
//

#include "GDisjointSet.h"
#include <iostream>

[[maybe_unused]] void minConnectedRoad() {
    int n, m;
    while (std::cin >> n) {
        if (n == 0) {
            break;
        }
        std::cin >> m;
        GDisjointSet set(n);
        for (int i = 0; i < m; i++) {
            int a, b;
            std::cin >> a >> b;
            set.unite(a - 1, b - 1);
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (set.find(i) == i) {
                count++;
            }
        }
        std::cout << count - 1 << std::endl;
    }
}
